package com.eorchis.module.trainingquestionnaire.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.eorchis.core.annotation.AuditModule;
import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.service.impl.AbstractBaseService;
import com.eorchis.module.trainingneeds.domain.QueryQuestionBankBean;
import com.eorchis.module.trainingneeds.domain.QueryQuestionnaireBean;
import com.eorchis.module.trainingneeds.domain.QuestionBank;
import com.eorchis.module.trainingneeds.domain.QuestionOptionBean;
import com.eorchis.module.trainingquestionnaire.dao.IQuestionnaireDao;
import com.eorchis.module.trainingquestionnaire.domain.ClassQuestionnaireSummary;
import com.eorchis.module.trainingquestionnaire.domain.PreviewQuestionnaireBean;
import com.eorchis.module.trainingquestionnaire.domain.QuestionnaireEntity;
import com.eorchis.module.trainingquestionnaire.domain.QuestionnaireQueryBean;
import com.eorchis.module.trainingquestionnaire.domain.userquestionnairebean.EssayQuestionInfo;
import com.eorchis.module.trainingquestionnaire.domain.userquestionnairebean.ItemOption;
import com.eorchis.module.trainingquestionnaire.domain.userquestionnairebean.ItemType;
import com.eorchis.module.trainingquestionnaire.domain.userquestionnairebean.PaperInfo;
import com.eorchis.module.trainingquestionnaire.domain.userquestionnairebean.SelectProblemInfo;
import com.eorchis.module.trainingquestionnaire.service.IQuestionnaireService;
import com.eorchis.module.trainingquestionnaire.ui.commond.QuestionnaireQueryCommond;
import com.eorchis.module.trainingquestionnaire.ui.commond.QuestionnaireValidCommond;
import com.eorchis.utils.DateUtil;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: QuestionnaireServiceImpl<br>
 * Description: 调查问卷管理业务接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Administrator
 * @createDate 2014-08-27
 * @version $Revision$
 */
// 声明本类为Server实现类，声明bean名称
@Service(value = "main.java.com.eorchis.module.trainingquestionnaire.service.impl.QuestionnaireServiceImpl")
@AuditModule("调查问卷")
public class QuestionnaireServiceImpl 
			extends AbstractBaseService 
		implements IQuestionnaireService {
		
	@Autowired
	@Qualifier("main.java.com.eorchis.module.trainingquestionnaire.dao.impl.QuestionnaireDaoImpl")
	private IQuestionnaireDao questionnaireDao;
	
	/*@Autowired
	@Qualifier("com.eorchis.webservice.resshow.common.service.ipl.ResRemindServiceIMpl")
	private ResRemindServiceIMpl resRemindServiceIMpl;*/
		

	public IDaoSupport getDaoSupport() {
		return questionnaireDao;
	}
	@SuppressWarnings("unchecked")
	public QuestionnaireValidCommond toCommond(IBaseEntity entity) {
		return new QuestionnaireValidCommond((QuestionnaireEntity)entity);
	}
	/**
	 * 更新问卷活动状态
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-7
	 */
	@Override
	public int updateQuestionnaireActiveState(QuestionnaireQueryCommond commond)
			throws Exception {
		return questionnaireDao.updateQuestionnaireActiveState(commond);
	}
	/**
	 * 查询活动问卷列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Xupp
	 * @createDate 2014-8-27
	 */
	public List<QuestionnaireQueryBean> queryListByActiveState(QuestionnaireQueryCommond commond)throws Exception{
		return questionnaireDao.queryListByActiveState(commond);
	}
//	/**
//	 * 查询活动问卷
//	 * @param queryCommond
//	 * @return
//	 * @throws Exception
//	 * @author Xupp
//	 * @createDate 2014-8-27
//	 */
//	public QuestionnaireQueryBean queryQuestionnaireInfo(QuestionnaireValidCommond commond)throws Exception{
//		return questionnaireDao.queryQuestionnaireInfo(commond);
//	}
	/**
	 * 发布撤销培训需求调查问卷
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-2-7
	 */
	@Override
	public String publishQuestionnaire(QuestionnaireQueryCommond queryCommond) throws Exception
	{
		if(queryCommond.getPublishState().equals(QuestionnaireEntity.IS_PUBLISH_Y))
		{
			if(!questionnaireDao.isPublish(queryCommond))
			{
				return "问卷必须设置试题后才能发布！";
			}
		}
//		else
//		{
//			String[] questionnaireIds=queryCommond.getSearchQuestionnaireIds();
//			for(String id: questionnaireIds)
//			{
//				QuestionnaireValidCommond c=this.find(id);
//				Date start=DateUtil.changeDate0H0m0s0ms(new Date());
//				Date end=DateUtil.changeDate23H59m59s999ms(new Date());
//				/*if(!c.getBeginDate().after(end)&&!c.getEndDate().before(start))
//				{
//					return "已开始的调查问卷，不能撤销发布！";
//				}*/
//			}
//		}
		if(questionnaireDao.publishQuestionnaire(queryCommond)>0)
		{
			return "操作成功";
		}
		else
		{
			return "操作失败";
		}
	}
	/**
	 * 预览问卷信息
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-5-28
	 */
	@Override
	public PreviewQuestionnaireBean previewQuestionnaire(QuestionnaireValidCommond commond) throws Exception
	{
		List<QueryQuestionBankBean> list=questionnaireDao.queryQuestionnaireQuestion(commond);
		Map<String, List<QuestionOptionBean>> optionMap=new LinkedHashMap<String, List<QuestionOptionBean>>();//选择题选项集合
		Map<String, QueryQuestionBankBean> questionMap=new LinkedHashMap<String, QueryQuestionBankBean>();//试题集合
		
		for(QueryQuestionBankBean bean: list)
		{
			if(!bean.getQuestionType().equals(QuestionBank.QUESTION_WEN))
			{
				QuestionOptionBean qo=new QuestionOptionBean();
				qo.setOptionNum(bean.getOptionNum());
				qo.setOptionName(bean.getOptionName());
				qo.setObjectiveTopic(bean.getObjectiveTopic());
				if(optionMap.containsKey(bean.getQuestionId()))
				{
					optionMap.get(bean.getQuestionId()).add(qo);
				}
				else
				{
					optionMap.put(bean.getQuestionId(), new ArrayList<QuestionOptionBean>());
					optionMap.get(bean.getQuestionId()).add(qo);
				}
			}
			if(!questionMap.containsKey(bean.getQuestionId()))
				questionMap.put(bean.getQuestionId(), bean);
				
		}
		List<QueryQuestionBankBean> singleChoiceList=new ArrayList<QueryQuestionBankBean>();//单选题集合 
		List<QueryQuestionBankBean> multipleChoiceList=new ArrayList<QueryQuestionBankBean>();//多选题集合 
		List<QueryQuestionBankBean> essayQuestionList=new ArrayList<QueryQuestionBankBean>();//问答题集合 
		
		for(Map.Entry<String, QueryQuestionBankBean> map: questionMap.entrySet())
		{
			QueryQuestionBankBean bean=map.getValue();
			
			if(!bean.getQuestionType().equals(QuestionBank.QUESTION_WEN))
			{
				List<QuestionOptionBean> optionList=optionMap.get(bean.getQuestionId());
				Collections.sort(optionList, new Comparator<QuestionOptionBean>() {
					@Override
					public int compare(QuestionOptionBean o1, QuestionOptionBean o2) 
					{
						return o1.getOptionNum().compareTo(o2.getOptionNum());
					}
				});
				bean.setQuestionOption(optionList);
			}
			
			if(QuestionBank.QUESTION_DAN.equals(bean.getQuestionType()))
			{
				singleChoiceList.add(bean);
			}
			else
				if(QuestionBank.QUESTION_DUO.equals(bean.getQuestionType()))
				{
					multipleChoiceList.add(bean);
				}
				else
					if(QuestionBank.QUESTION_WEN.equals(bean.getQuestionType()))
					{
						essayQuestionList.add(bean);
					}
		}
		//试题排序
		this.sortQuestionList(singleChoiceList);
		this.sortQuestionList(multipleChoiceList);
		this.sortQuestionList(essayQuestionList);
		
		QuestionnaireEntity q=this.getDaoSupport().find(QuestionnaireEntity.class, commond.getEntityID());
		PreviewQuestionnaireBean previewQuestionnaireBean=new PreviewQuestionnaireBean();
		
		previewQuestionnaireBean.setQuestionnaireId(commond.getQuestionnaireId());
		previewQuestionnaireBean.setSingleChoiceList(singleChoiceList);
		previewQuestionnaireBean.setMultipleChoiceList(multipleChoiceList);
		previewQuestionnaireBean.setEssayQuestionList(essayQuestionList);
		if(PropertyUtil.objectNotEmpty(q))
		{
			previewQuestionnaireBean.setQuestionnaireName(q.getQuestionnaireName());
			previewQuestionnaireBean.setQuestionnaireExplain(q.getQuestionnaireExplain());
		}
		
		return previewQuestionnaireBean;
	}
	/**
	 * 对试题集合排序
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-5-28
	 */
	public void sortQuestionList(List<QueryQuestionBankBean> list)
	{
		if(PropertyUtil.objectNotEmpty(list))
		{
			Collections.sort(list, new Comparator<QueryQuestionBankBean>() {
				@Override
				public int compare(QueryQuestionBankBean o1, QueryQuestionBankBean o2) 
				{
					return o1.getOrderNum().compareTo(o2.getOrderNum());
				}
			});
		}
	}
	/**
	 * 统计分析，问卷列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-4-29
	 */
	@Override
	public List<QueryQuestionnaireBean> findEndQuestionnaireList(QuestionnaireQueryCommond queryCommond) throws Exception
	{
		queryCommond.setActiveState(QuestionnaireQueryCommond.IS_ENABLE);
		List<QueryQuestionnaireBean> beanList=questionnaireDao.findEndQuestionnaireList(queryCommond);
		//设置计划调查人数
		for(QueryQuestionnaireBean bean: beanList)
		{
			queryCommond.setSearchQuestionnaireId(bean.getQuestionnaireId());
			bean.setPlanSurveyUserNum(questionnaireDao.querySurveyObjectNum(queryCommond).size());
		}
		return beanList;
	}
	/**
	 * 统计分析，查询问卷计划调查人员Id
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-09-02
	 */
	@Override
	public List<String> querySurveyObjectNum(QuestionnaireQueryCommond queryCommond) throws Exception
	{
		return questionnaireDao.querySurveyObjectNum(queryCommond);
	}
	/**
	 * 门户提醒信息，推送问卷开始提醒信息到门户
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-09-11
	 */
	@Override
	public String sendStartQuestionnaireMess(QuestionnaireQueryCommond queryCommond) throws Exception
	{
		String mess="";
		String[] questionnaireIds=questionnaireDao.queryQuestionnaireNeedSendInfo();
		if(PropertyUtil.objectNotEmpty(questionnaireIds))
		{
			queryCommond.setSearchQuestionnaireIds(questionnaireIds);
			
			mess=this.queryQuestionnaireSurveyScope(queryCommond);
		}
		return mess;
	}
	/**
	 * 门户提醒信息，问卷开始后推送提醒信息到门户
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-09-04
	 */
	@Override
	public String queryQuestionnaireSurveyScope(QuestionnaireQueryCommond queryCommond) throws Exception
	{
		return null;
		/*List<TrainNeedScope> trainNeedScopeList=questionnaireDao.queryQuestionnaireSurveyScope(queryCommond);
		Map<String, List<Map<String,List<String>>>> deptScope=new HashMap<String, List<Map<String,List<String>>>>();
		Map<String, List<String>> userScope=new HashMap<String, List<String>>();
		for(TrainNeedScope surveyObject: trainNeedScopeList)
		{
			String questionnaireId=surveyObject.getQuestionnaire().getQuestionnaireId();
			if(surveyObject.getSurveyObjectType().intValue()==TrainNeedScope.SURVER_TYPE_USER.intValue())
			{
				if(!userScope.containsKey(questionnaireId))
				{
					userScope.put(questionnaireId, new ArrayList<String>());
				}
				userScope.get(questionnaireId).add(surveyObject.getSurveyObjectId());
			}
			else
			{
				if(!deptScope.containsKey(questionnaireId))
				{
					deptScope.put(questionnaireId, new ArrayList<Map<String,List<String>>>());
				}
				List<String> dutyList=new ArrayList<String>();
				if(PropertyUtil.objectNotEmpty(surveyObject.getDutyCodes()))
				{
					dutyList=Arrays.asList(surveyObject.getDutyCodes().split(","));
				}
				Map<String,List<String>> map=new HashMap<String,List<String>>();
				map.put(surveyObject.getSurveyObjectId(), dutyList);
				deptScope.get(questionnaireId).add(map);
			}
		}
		List<ResRemindBean> beanList=new ArrayList<ResRemindBean>();
		
		for(Map.Entry<String, List<Map<String,List<String>>>> map: deptScope.entrySet())
		{
			ResRemindBean bean=new ResRemindBean();
			bean.setScopeType(ResRemindConstants.SCOPE_TYPE_DEPTDUTY);
			bean.setEntityId(map.getKey());
			bean.setRemindType(ResRemindConstants.REAMIND_ONLINE_TRAINING_NEEDS_QUESTIONNAIRE);
			
			String scopeList=resRemindServiceIMpl.getScopeList(ResRemindConstants.SCOPE_TYPE_DEPTDUTY, Arrays.asList(map.getValue().toArray()));
			bean.setScopeList(scopeList);
			
			beanList.add(bean);
		}
		for(Map.Entry<String, List<String>> map: userScope.entrySet())
		{
			ResRemindBean bean=new ResRemindBean();
			bean.setScopeType(ResRemindConstants.SCOPE_TYPE_USER);
			bean.setEntityId(map.getKey());
			bean.setRemindType(ResRemindConstants.REAMIND_ONLINE_TRAINING_NEEDS_QUESTIONNAIRE);
			
			String scopeList=resRemindServiceIMpl.getScopeList(ResRemindConstants.SCOPE_TYPE_USER, Arrays.asList(map.getValue().toArray()));
			bean.setScopeList(scopeList);
			
			beanList.add(bean);
		}
		
		String mess=resRemindServiceIMpl.addResRemindBatch(beanList);
		return mess;*/
	}
	/**
	 * 判断用户是否已经参加调查问卷
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-09-10
	 */
	@Override
	public boolean isJoinQuestionnaire(QuestionnaireQueryCommond queryCommond) throws Exception
	{
		return questionnaireDao.isJoinQuestionnaire(queryCommond);
	}
	/**
	 * 发布问卷时，推送问卷开始提醒信息到门户
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2014-09-10
	 */
	@Override
	public String sendStartMessInPublish(QuestionnaireQueryCommond queryCommond) throws Exception
	{
		String[] questionnaireIds=queryCommond.getSearchQuestionnaireIds();
		String needSendMessIds="";
		for(String id: questionnaireIds)
		{
			QuestionnaireValidCommond c=this.find(id);
			Date start=DateUtil.changeDate0H0m0s0ms(new Date());
			Date end=DateUtil.changeDate23H59m59s999ms(new Date());
			if(!c.getBeginDate().after(end)&&!c.getEndDate().before(start))
//			if(!c.getBeginDate().after(new Date())&&!c.getEndDate().before(new Date()))
			{
				needSendMessIds+=id+",";
			}
		}
		if(needSendMessIds.equals(""))
		{
			return "";
		}
		needSendMessIds=needSendMessIds.substring(0,needSendMessIds.length()-1);
		queryCommond.setSearchQuestionnaireIds(needSendMessIds.split(","));
		String mess=this.queryQuestionnaireSurveyScope(queryCommond);
		
		return mess;
	}
	/**
	 * 构建调查问卷
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @date 2014-11-05
	 */
	@Override
	public PaperInfo createQuestionnaireInfo(QuestionnaireValidCommond commond) throws Exception
	{
		PreviewQuestionnaireBean questionnaireInfo=this.previewQuestionnaire(commond);
		PaperInfo pageInfo=new PaperInfo();
		List<ItemType> itemType=new ArrayList<ItemType>();
		//单选题
		List<SelectProblemInfo> selectProblemInfo=new ArrayList<SelectProblemInfo>();
		if(PropertyUtil.objectNotEmpty(questionnaireInfo.getSingleChoiceList()))
		{
			ItemType it1=new ItemType();
			it1.setItemTypeName("QETT06");
			it1.setSumNum(questionnaireInfo.getSingleChoiceList().size());
			itemType.add(it1);
			for(QueryQuestionBankBean bean: questionnaireInfo.getSingleChoiceList())
			{
				SelectProblemInfo s=new SelectProblemInfo();
				
				s.setItemId(bean.getQuestionId());
				s.setItemTitle(bean.getQuestionName());
				s.setIsScoreType(bean.getIsScoreType());
				List<ItemOption> itemOption=new ArrayList<ItemOption>();
				for(QuestionOptionBean option: bean.getQuestionOption())
				{
					ItemOption io=new ItemOption();
					io.setOptionNum(option.getOptionNum());
					io.setOption(option.getOptionName());
					io.setIsSelect(option.isSelect());
					itemOption.add(io);
				}
				s.setItemOption(itemOption);
				s.setIsMust(bean.getIsMust());
				s.setQuestionsProblemSets(bean.getQuestionsProblemSets());//题组
				selectProblemInfo.add(s);
			}
		}
		//多选题
		List<SelectProblemInfo> multipleSelectProblemInfo=new ArrayList<SelectProblemInfo>();
		if(PropertyUtil.objectNotEmpty(questionnaireInfo.getMultipleChoiceList()))
		{
			ItemType it2=new ItemType();
			it2.setItemTypeName("QETT01");
			it2.setSumNum(questionnaireInfo.getMultipleChoiceList().size());
			itemType.add(it2);
			for(QueryQuestionBankBean bean: questionnaireInfo.getMultipleChoiceList())
			{
				SelectProblemInfo s=new SelectProblemInfo();
				
				s.setItemId(bean.getQuestionId());
				s.setItemTitle(bean.getQuestionName());
				List<ItemOption> itemOption=new ArrayList<ItemOption>();
				for(QuestionOptionBean option: bean.getQuestionOption())
				{
					ItemOption io=new ItemOption();
					io.setOptionNum(option.getOptionNum());
					io.setOption(option.getOptionName());
					io.setIsSelect(option.isSelect());
					io.setObjectiveTopic(option.getObjectiveTopic());
					itemOption.add(io);
				}
				s.setItemOption(itemOption);
				s.setIsMust(bean.getIsMust());
				multipleSelectProblemInfo.add(s);
			}
		}
		List<EssayQuestionInfo> essayQuestionInfo=new ArrayList<EssayQuestionInfo>();//问答题集合 
		//问答题
		if(PropertyUtil.objectNotEmpty(questionnaireInfo.getEssayQuestionList()))
		{
			ItemType it3=new ItemType();
			it3.setItemTypeName("QETT02");
			it3.setSumNum(questionnaireInfo.getEssayQuestionList().size());
			itemType.add(it3);
			for(QueryQuestionBankBean bean: questionnaireInfo.getEssayQuestionList())
			{
				EssayQuestionInfo eq=new EssayQuestionInfo();
				eq.setQuestionId(bean.getQuestionId());
				eq.setQuestionName(bean.getQuestionName());
				eq.setIsMust(bean.getIsMust());
				if(PropertyUtil.objectNotEmpty(bean.getWordNum()))
					eq.setWordNum(bean.getWordNum()/2);
				eq.setOrderNum(bean.getOrderNum());
				essayQuestionInfo.add(eq);
			}
		}
		
		pageInfo.setPageId(questionnaireInfo.getQuestionnaireId());
		pageInfo.setPapeTitle(questionnaireInfo.getQuestionnaireName());
		pageInfo.setItemType(itemType);
		pageInfo.setSelectProblemInfo(selectProblemInfo);
		pageInfo.setMultipleSelectProblemInfo(multipleSelectProblemInfo);
		pageInfo.setEssayQuestionInfo(essayQuestionInfo);
		pageInfo.setQuestionnaireExplain(questionnaireInfo.getQuestionnaireExplain());
		
		return pageInfo;
	}
	
	/**
	 * 配置问卷列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Xupp
	 * @createDate 2014-8-27  LEFT  QuestionnaireSurveyObject  qs
	 */
	@Override
	public List<QuestionnaireQueryBean> configureQuestionnaire(
			QuestionnaireQueryCommond commond) throws Exception {
		return questionnaireDao.configureQuestionnaire(commond);
	}
	
	/**
	 * 配置课程问卷列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Xupp
	 * @createDate 2014-8-27  LEFT  QuestionnaireSurveyObject  qs
	 */
	@Override
	public List<QuestionnaireQueryBean> configureCourseQuestionnaire(
			QuestionnaireQueryCommond commond) throws Exception {
		return questionnaireDao.configureCourseQuestionnaire(commond);
	}
	
	/**
	 * @查询评估开始和结束时间
	  * @Name: assessTime
	  * @Description: @param queryCommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Description: @throws Exception
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-2-5下午2:19:45
	  * @Parameters: QuestionnaireController
	  * @Return: String
	 */
	public QuestionnaireQueryBean assessTime(QuestionnaireValidCommond validcommond)
					throws Exception{
		return questionnaireDao.assessTime(validcommond);
		
	}
	
	/**
	 * @查询评估开始和结束时间
	  * @Name: assessTime
	  * @Description: @param queryCommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Description: @throws Exception
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-2-5下午2:19:45
	  * @Parameters: QuestionnaireController
	  * @Return: String
	 */
	@Override
	public QuestionnaireQueryBean assessCourseTime(QuestionnaireValidCommond validcommond)
					throws Exception{
		return questionnaireDao.assessCourseTime(validcommond);
		
	}
	
	/**
	 * @更新评估开始时间和评估结束时间
	  * @Name: updateAssessTime
	  * @Description: @param validcommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Description: @throws Exception
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-2-5下午2:53:27
	  * @Parameters: QuestionnaireController
	  * @Return: String
	 */
	public String updateAssessTime(QuestionnaireValidCommond validcommond)throws Exception{
		return questionnaireDao.updateAssessTime(validcommond);
	}
	
	/**
	 * @更新评估开始时间和评估结束时间
	  * @Name: updateAssessTime
	  * @Description: @param validcommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Description: @throws Exception
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-2-5下午2:53:27
	  * @Parameters: QuestionnaireController
	  * @Return: String
	 */
	public String updateCourseAssessTime(QuestionnaireValidCommond validcommond)throws Exception{
		return questionnaireDao.updateCourseAssessTime(validcommond);
	}
	
	/**
	 * @删除问卷
	  * @Name: updateAssessTime
	  * @Description: @param validcommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Description: @throws Exception
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-2-5下午2:53:27
	  * @Parameters: QuestionnaireController
	  * @Return: String
	 */
	public String deleteQuestionnaires(QuestionnaireValidCommond validcommonde)throws Exception{
		return questionnaireDao.deleteQuestionnaires(validcommonde);
	}
	
	
	/**
	 * 问卷列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author GUOLEI
	 * @createDate 2014-8-27  LEFT  QuestionnaireSurveyObject  qs
	 */
	public List<QuestionnaireQueryBean> queryUserQuestionnaireList( QuestionnaireQueryCommond commond) throws Exception {
		return  questionnaireDao.queryUserQuestionnaireList(commond);
	}
	
	/**
	 * @throws Exception 
	 * @分题组
	  * @Name: problemSets
	  * @Description: @param commond
	  * @Description: @return
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-2-14下午4:56:55
	  * @Parameters: QuestionnaireServiceImpl
	  * @Return: List<PaperInfo>
	 */
	public List<List<SelectProblemInfo>>  problemSets(List<SelectProblemInfo> selectProblemInfo) throws Exception{
		Map<String, List<SelectProblemInfo>> map = new LinkedHashMap<String, List<SelectProblemInfo>>();
		List<List<SelectProblemInfo>>  selectProblemInfoList =new ArrayList<List<SelectProblemInfo>>();
		if(PropertyUtil.objectNotEmpty(selectProblemInfo)){
			for(SelectProblemInfo selectProblem: selectProblemInfo){
				if(PropertyUtil.objectNotEmpty(map) && map.size()>0){
					int i=0;
					for(String key:map.keySet()){
						i++;
						if(PropertyUtil.objectNotEmpty(selectProblem.getQuestionsProblemSets())){
								if(selectProblem.getQuestionsProblemSets().trim().equals(key)){
									map.get(key).add(selectProblem);
									break;
								}
								if(i==map.size()){
									List<SelectProblemInfo> queryQuestionBankList =new ArrayList<SelectProblemInfo>();
									queryQuestionBankList.add(selectProblem);
									map.put(selectProblem.getQuestionsProblemSets().trim(), queryQuestionBankList);
									break;
								}
						
							}else{
								if(PropertyUtil.objectNotEmpty(map.get(null))){
									map.get(null).add(selectProblem);
								}else{
									List<SelectProblemInfo> queryQuestionBankList =new ArrayList<SelectProblemInfo>();
									queryQuestionBankList.add(selectProblem);
									map.put(null, queryQuestionBankList);
								}
								break;
							}
						
					}
				}else{
					List<SelectProblemInfo> queryQuestionBankList =new ArrayList<SelectProblemInfo>();
					queryQuestionBankList.add(selectProblem);
					if(PropertyUtil.objectNotEmpty(selectProblem.getQuestionsProblemSets())){
						map.put(selectProblem.getQuestionsProblemSets().trim(), queryQuestionBankList);
					}
					else{
						map.put(null, queryQuestionBankList);
					}
				}
			}
			
			
				for(String rKey:map.keySet()){
					selectProblemInfoList.add(map.get(rKey));
				}
			}
		return  selectProblemInfoList;
	}
	
	//add RongSL
	public List<List<SelectProblemInfo>>  ExportProblemSets(List<SelectProblemInfo> selectProblemInfo) throws Exception{
		Map<String, List<SelectProblemInfo>> map = new LinkedHashMap<String, List<SelectProblemInfo>>();
		List<List<SelectProblemInfo>>  selectProblemInfoList =new ArrayList<List<SelectProblemInfo>>();
		if(PropertyUtil.objectNotEmpty(selectProblemInfo)){
			for(SelectProblemInfo selectProblem: selectProblemInfo){
				if(PropertyUtil.objectNotEmpty(map) && map.size()>0){
					int i=0;
					for(String key:map.keySet()){
						i++;
						if(PropertyUtil.objectNotEmpty(selectProblem.getQuestionsProblemSets())){
								if(selectProblem.getQuestionsProblemSets().trim().equals(key)){
									map.get(key).add(selectProblem);
									break;
								}
								if(i==map.size()){
									List<SelectProblemInfo> queryQuestionBankList =new ArrayList<SelectProblemInfo>();
									queryQuestionBankList.add(selectProblem);
									map.put(selectProblem.getQuestionsProblemSets().trim(), queryQuestionBankList);
									break;
								}
						
							}else{
								if(PropertyUtil.objectNotEmpty(map.get(null))){
									map.get(null).add(selectProblem);
								}else{
									List<SelectProblemInfo> queryQuestionBankList =new ArrayList<SelectProblemInfo>();
									selectProblem.setQuestionsProblemSets("无试题题组");
									queryQuestionBankList.add(selectProblem);
									map.put("无试题题组", queryQuestionBankList);
								}
								break;
							}
						
					}
				}else{
					List<SelectProblemInfo> queryQuestionBankList =new ArrayList<SelectProblemInfo>();
					queryQuestionBankList.add(selectProblem);
					if(PropertyUtil.objectNotEmpty(selectProblem.getQuestionsProblemSets())){
						map.put(selectProblem.getQuestionsProblemSets().trim(), queryQuestionBankList);
					}
					else{
						selectProblem.setQuestionsProblemSets("无试题题组");
						map.put("无试题题组", queryQuestionBankList);
					}
				}
			}
			
			
				for(String rKey:map.keySet()){
					selectProblemInfoList.add(map.get(rKey));
				}
			}
		return  selectProblemInfoList;
	}
	
	/**
	 * 查询课程信息
	 * @author RongSL
	 * @Date 2016-05-25
	 * */
	@Override
	public QuestionnaireValidCommond findCourseInfo(QuestionnaireValidCommond commond) throws Exception{
		return questionnaireDao.findCourseInfo(commond);
	}
	/**
	 * @author RongSL
	 * @Date 2016-05-26
	 * 根据课程id找到相关班级id
	 * */
	@Override
	public String findClassId(QuestionnaireQueryCommond commond) throws Exception{
		return questionnaireDao.findClassId(commond);
	}
	
	/**
	 * 查询课程汇总数据
	 * @Date 2016-09-27
	 * @author RongSL
	 * */
	public List<ClassQuestionnaireSummary> findClassCourseQuestionSummary(QuestionnaireQueryCommond commond) throws Exception{
		List<ClassQuestionnaireSummary> list=groupByClassCourseList(questionnaireDao.findClassCourseQuestionSummary(commond));
		List<ClassQuestionnaireSummary> remarksList=groupByQuestionAnswerList(questionnaireDao.findClassCourseQuestionSummaryRemarks(commond));//问答题列表
		for(ClassQuestionnaireSummary c:list){
			for(ClassQuestionnaireSummary c2:remarksList){
				if(c.getClassCourseId().equals(c2.getClassCourseId())){
					c.setQuestionAnswers(c2.getQuestionAnswers());
					c.setQuestionAnswerTexts(c2.getQuestionAnswerTexts());
				}
			}
		}
		return list;
	}
	
	//根据课程进行分组
	public List<ClassQuestionnaireSummary> groupByClassCourseList( List<ClassQuestionnaireSummary> list){
		
		 List<ClassQuestionnaireSummary> list1=new ArrayList<ClassQuestionnaireSummary>();
		Map<String,ClassQuestionnaireSummary> map=new LinkedHashMap<String,ClassQuestionnaireSummary>();
		ClassQuestionnaireSummary cq;
		for(ClassQuestionnaireSummary c:list){
			if(PropertyUtil.objectNotEmpty(map.get(c.getClassCourseId()))){
				cq=map.get(c.getClassCourseId());
				cq.getQuestionNames().add(c.getQuestionName());
				cq.getAverages().add(c.getAverage());
//				cq.setCount(cq.getCount()+c.getCount());
			}else{
				c.getQuestionNames().add(c.getQuestionName());
				c.getAverages().add(c.getAverage());
				map.put(c.getClassCourseId(), c);
			}
		}
		for(ClassQuestionnaireSummary cqs:map.values()){
			list1.add(cqs);
		}
		return list1;
	}
	
	//问答题分组
	public List<ClassQuestionnaireSummary> groupByQuestionAnswerList( List<ClassQuestionnaireSummary> list){
		
		List<ClassQuestionnaireSummary> list1=new ArrayList<ClassQuestionnaireSummary>();
		List<ClassQuestionnaireSummary> list2=new ArrayList<ClassQuestionnaireSummary>();
		Map<String,List<ClassQuestionnaireSummary>> map=new LinkedHashMap<String,List<ClassQuestionnaireSummary>>();
		//按问答题进行分组
		Map<String,ClassQuestionnaireSummary> map1=new LinkedHashMap<String,ClassQuestionnaireSummary>();
		//最终结果
		ClassQuestionnaireSummary cq = null;
		//按课程分组
		for(ClassQuestionnaireSummary c:list){
			if(PropertyUtil.objectNotEmpty(map.get(c.getClassCourseId()))){
				list1=map.get(c.getClassCourseId());
				list1.add(c);
				map.put(c.getClassCourseId(), list1);
			}else{
				list1=new ArrayList<ClassQuestionnaireSummary>();
				list1.add(c);
				map.put(c.getClassCourseId(),list1 );
			}
		}
//		list1.clear();
		//按问答题进行分组
		for(String cqs:map.keySet()){
			String qId = null;
			for(ClassQuestionnaireSummary c:map.get(cqs)){
				qId=c.getQuestionId();
				if(PropertyUtil.objectNotEmpty(map1.get(c.getQuestionId()))){
					cq=map1.get(c.getQuestionId());
					for(int i=0;i<cq.getQuestionAnswers().size();i++){
						if(cq.getQuestionAnswers().get(i).equals(c.getQuestionAnswer())){
							cq.setQuestionAnswerText((PropertyUtil.objectNotEmpty(cq.getQuestionAnswerText())?cq.getQuestionAnswerText()+"\r\n":"")+(PropertyUtil.objectNotEmpty(c.getQuestionAnswerText())?c.getQuestionAnswerText()+"\r\n":""));
							cq.getQuestionAnswerTexts().set(i, cq.getQuestionAnswerText());
						}
					}
//					cq.getQuestionAnswers().add(c.getQuestionAnswer());
//					cq.getQuestionAnswerTexts().add(c.getQuestionAnswerText());
					map1.put(c.getQuestionId(), cq);
				}else{
					cq=c;
					cq.getQuestionAnswers().add(c.getQuestionAnswer());
					cq.getQuestionAnswerTexts().add(c.getQuestionAnswerText());
					map1.put(c.getQuestionId(), cq);
				}
			}
			list2.add(cq);
			map1.remove(qId);
		}
		return list2;
	}
	
	/**
	 * 查询培训班汇总数据
	 * @Date 2016-10-08
	 * @author RongSL
	 * */
	@Override
	public List<ClassQuestionnaireSummary> findClassQuestionSummaryRemarks(QuestionnaireQueryCommond commond) throws Exception {
//		List l=groupByClassList(this.questionnaireDao.findClassQuestionSummaryRemarks(commond));
		return this.questionnaireDao.findClassQuestionSummaryRemarks(commond);
	}
	
		//根据班级进行分组
		public List<ClassQuestionnaireSummary> groupByClassList( List<ClassQuestionnaireSummary> list){
			 List<ClassQuestionnaireSummary> list1=new ArrayList<ClassQuestionnaireSummary>();
			Map<String,ClassQuestionnaireSummary> map=new LinkedHashMap<String,ClassQuestionnaireSummary>();
			ClassQuestionnaireSummary cq;
			for(ClassQuestionnaireSummary c:list){
				if(PropertyUtil.objectNotEmpty(map.get(c.getClassId()))){
					cq=map.get(c.getClassId());
					cq.getQuestionNames().add(c.getQuestionName());
					cq.getAverages().add(c.getAverage());
					
					//TODO 需要改变写法
					cq.setCount(BigInteger.valueOf(Long.valueOf((cq.getCount()+c.getCount()+""))));
				}else{
					c.getQuestionNames().add(c.getQuestionName());
					c.getAverages().add(c.getAverage());
					map.put(c.getClassId(), c);
				}
			}
			for(ClassQuestionnaireSummary cqs:map.values()){
				list1.add(cqs);
			}
			return list1;
		}
}
